package com.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {


    /**
     * 获取过去12个自然月
     * @return
     */
    public static String[] getLast12Months() {
        String[] months = new String[12];
        LocalDate today = LocalDate.now();
        for (int i = 12; i > 0; i--) {
            /*LocalDate localDate = today.minusMonths(i);
            months[12 - i] = localDate.toString();*/
            LocalDate localDate = today.minusMonths(i-1);
            String yearAndMonth = localDate.getYear() + "-" + (localDate.getMonthValue()<10?"0"+localDate.getMonthValue():localDate.getMonthValue());
            months[12 - i] = yearAndMonth;
        }
        return months;
    }

    /**
     * 获取本月截止至今天的日期
     * @return
     */
    public static String[] getCurrentDayByThisMonth(){
        //截止当前时间
        Date current = new Date();
        //获取本月截止当前已过的日期数,+1匹配当日
        int dayTotal = (int) DateUtil.betweenDay(DateUtil.beginOfMonth(current), current, true) + 1;

        //创建日期数组
        String[] days = new String[dayTotal];

        //获取具体日期
        for(int i = dayTotal ; i > 0 ; i--){
            days[days.length - i] = DateUtil.format(DateUtil.offsetDay(current, -(i-1)), DatePattern.NORM_DATE_PATTERN);
        }

        return days;
    }

    /**
     * 获取 当月所有的日期集合 wgh
     * @return
     */
    public static String[] getDayListOfMonth() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
//        aCalendar.set(2020,ueditor,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        //创建日期数组
        String[] days = new String[day];
        for (int i = 1; i <= day; i++) {
            String days2="0";
            if (i<10){
                days2="0"+i;
            }else {
                days2=String.valueOf(i);
            }
            days[i-1] =String.valueOf(year)+"-"+month+"-"+days2;
        }
        return days;
    }
    public static void main(String[] args) {
        for (String item : getDayListOfMonth()){
            System.out.println(item);
        }
    }


    /**
     * 获取某个月的最后一天
     * @param dateStr  yyyy-MM-dd
     * @return
     */
    public static String getMonthEnd(String dateStr) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateStr);
            Calendar c = Calendar.getInstance();
            c.setTime(date);

            //设置为当月最后一天
            c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
            //将小时至23
            c.set(Calendar.HOUR_OF_DAY, 23);
            //将分钟至59
            c.set(Calendar.MINUTE, 59);
            //将秒至59
            c.set(Calendar.SECOND,59);
            //将毫秒至999
            c.set(Calendar.MILLISECOND, 999);
            // 获取本月最后一天的时间戳

            return sdf.format(new Date(c.getTimeInMillis()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String date = sdf.format(new Date());
//        String[] months = TimeUtil.getLast12Months();
//        for (String month : months) {
//            System.out.println(TimeUtil.getMonthEnd(month));
//        }
//    }
}
